 <template>
  <div class="bg_page">
    <header-bar title="我的收藏" :interceptReturn="true" @return="returnBack"></header-bar>
    <pull-refresh :bottom="panelBottom" ref="pullRefreshPanel" id="scrollPanel" :background-color="'#f6f6f6'" :top="headerHeight" :enableRefresh="true" @refresh="refreshData">

      <div class="bottom_block">
        <div class="collection_list_block">
          <ul class="collection_list">

            <li class="collection_item" v-for="(item,index) in effectiveCollectionList" :key="item.id" @click="toGoodsDetail(item,1)" v-lockClick>

              <div class="item_top">
                <div class="user_msg">
                  <img class="user_avatar" v-lazy="item.userIcon">
                  <div class="right_msg">
                      <span class="user_name">{{item.nickName || item.userName}}</span>
                      <div class="push_time">
                        <span v-if="item.putTime">发布于{{item.putTime | putTimeFilter}}前</span>
                        &nbsp;{{item.city}}&nbsp;{{item.region}}
                      </div>
                  </div>
                </div>
                <div class="goods_price">
                  <span>￥</span>{{item.price}}
                </div>
              </div>
  
              <div class="goods_name pf_m">{{item.title}}</div>
              <div class="good_imgs">
                <img v-for="img in item.imgUrls.split(',').slice(0,3)" :key="img" v-lazy="img">
              </div>
              <div class="other_msg">
                <div class="location_msg"></div>
                <button class="cancel_btn" @click.stop="cancelCollect(item.goodsId,index,1)">取消收藏</button>
              </div>

            </li>
            <li class="collection_item" v-for="(item,index) in invalidCollectionList" :key="item.id" @click="toGoodsDetail(item,2)" v-lockClick>
              <img v-if="item.status == 'sold'" class="status_img" src="@/assets/imgs/common/saled_icon.png">
              <img v-if="item.status == 'off'" class="status_img" src="@/assets/imgs/common/dowself_icon.png">

              <div class="item_top">
                <div class="user_msg">
                  <img class="user_avatar" v-lazy="item.userIcon">
                  <div class="right_msg">
                      <span class="user_name">{{item.nickName || item.userName}}</span>
                      <div class="push_time">
                        <span v-if="item.putTime">发布于{{item.putTime | putTimeFilter}}天前</span>
                        &nbsp;{{item.city}}&nbsp;{{item.region}}
                      </div>
                  </div>
                </div>
                <div class="goods_price">
                  <!-- <span>￥</span>{{item.price}} -->
                </div>
              </div>
              
              <div class="goods_name pf_m disabled">{{item.title}}</div>
              <div class="good_imgs">
                <!-- <img v-for="img in item.imgUrls.split(',').slice(0,3)" :key="img" v-lazy="img"> -->
                <div class="goods_img_box bic" v-for="img in item.imgUrls.split(',').slice(0,3)" :key="img"  v-lazy:background-image="img"></div>
              </div>
              <div class="other_msg">
                <div class="location_msg"></div>
                <button class="cancel_btn" @click.stop="cancelCollect(item.goodsId,index,2)">取消收藏</button>
              </div>
            </li>

          </ul>
        </div>
        
        <div class="no_data_block" v-if="effectiveCollectionList.length == 0 && invalidCollectionList.length == 0 && !isLoading">
          <no-data-panel :tips-data="{text:'还没有已收藏的商品哦'}"></no-data-panel>
        </div>
        
        <!-- <div>
          <infinite-load v-if="effectiveCollectionList.length > 0 || invalidCollectionList.length > 0" :scroll-target="scrollTarget" :is-loading="isLoading" :tipsShow="tipsShow" :is-nomore="isNomre" @load="getNextData"></infinite-load>
        </div> -->
      </div>

    </pull-refresh>
  </div>
</template>

<script>
import HeaderBar from "@/components/header.vue"
import PullRefresh from "@/components/pullRefresh.vue";
import infiniteLoad from '@/components/infiniteLoad.vue';
import {mapState,mapActions} from 'vuex';
import native from '@/libs/native/index';
export default {
  props: {
   
  },
  components:{
    HeaderBar,
    PullRefresh,
    infiniteLoad,
  },
  mounted() {

  },
  data() {
    return {
      panelBottom:0,
      scrollTarget: '#scrollPanel',

      isLoading: false,
      isNomre: false,
      tipsShow: false,

      effectiveCollectionList:[],
      invalidCollectionList:[],
      currentPage:1
    }
  },

  computed: mapState({
    headerHeight: state => state.headerHeight,
  }),

  created() {
    this.getMyGoodsCollections();
  },
  filters:{
    putTimeFilter(val){
      if(val){
        let l = val.length;
        let type = val.charAt(l-1).toLowerCase();
        let num = val.slice(0,-1);
        if(type == 'm'){
          return num + '分钟';
        }else if(type == 'h'){
          return num + '小时';
        }else{
          return num + '天';
        }
      }else{
        return '';
      }
    }
  },
  methods: {
    ...mapActions(['toggleLoading','showMsg','showModal']),
    getMyGoodsCollections(done){
      if(!done && this.currentPage == 1){
        this.toggleLoading({flag:true});
        this.isLoading = true;
      }
      this.$ajax.getMyGoodsCollections({}).then((res)=>{
        this.toggleLoading({flag:false});
        if(this.isLoading){
          this.isLoading = false;
        };
        if(done){
          done()
        }
        if(res.code == '0000'){
          this.effectiveCollectionList = res.data.effectiveCollectionList || [];
          this.invalidCollectionList = res.data.invalidCollectionList || [];
        };
      });
    },

    refreshData(done){
      this.isLoading = false;
      this.isNomre =  false;
      this.tipsShow = false;
      this.currentPage = 1;
      this.getMyGoodsCollections(done)
    },

    getNextData(){
      this.isLoading = true;
      this.currentPage++;
      this.getMyGoodsCollections()
    },

    returnBack(){
      native.closeWebPage();
    },

    toGoodsDetail(item,type){
      if(type == 1){
        native.goGoodsDetail(item.goodsId);
      }else{
        if(item.status == 'sold'){
          this.showMsg({ content: '商品已售出'});
        }else if(item.status == 'off'){
          this.showMsg({ content: '商品已下架'});
        }else{
          this.showMsg({ content: '商品已失效'});
        } 
      }
    },

    cancelCollect(id,index,type){
      this.toggleLoading({flag:true});
      this.$ajax.cancelCollection({
       goodsId:id,
       type:'CANCEL'
      }).then((res)=>{
        this.toggleLoading({flag:false});
        if(res.code == '0000'){
          this.showMsg({ content: '取消成功'});
          if(type == 1){
            this.effectiveCollectionList.splice(index,1);
          }else{
            this.invalidCollectionList.splice(index,1);
          };
        };
      });
    }
  }
}
</script>

<style lang="less" scoped>
.bg_page{
  min-height: 100%;
}

.bottom_block{
   width: 100%;
   position: relative;
  //  top:-0.9rem;
}
.collection_list_block{
  background-color: #f6f6f6;
}
.collection_list{


  .collection_item{
    // height: 5.58rem;
    background-color: #fff;
    padding:0.2rem;
    padding-bottom: 0.3rem;
    margin-bottom: 0.2rem;
    position: relative;
    .status_img{
      position: absolute;
      top:0;
      width: 2.2rem;
      height: 1.8rem;
      right: 0.2rem;
    }
    .item_top{
      height: 0.8rem;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 0.2rem;
      position: relative;
      .user_msg{
        display: flex;
        align-items: center;
        .user_avatar{
          width: 0.72rem;
          height: 0.72rem;
          border-radius: 50%;
        }
        .right_msg{
          margin-left: 0.1rem;
          display: flex;
          flex-direction: column;
          .user_name{
            font-size:0.28rem;
            line-height: 0.36rem;
            color: #333;
          }
          .push_time{
            width: 4rem;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            margin-top: 0.08rem;
            font-size: 0.22rem;
            color: #999;
            line-height: 0.28rem;
          }
        }
      }
      .goods_price{
        font-size: 0.36rem;
        line-height: 0.44rem;
        color: #FF4238;
        // display: flex;
        // align-items: flex-end;
        // font-family: din-bold;
        position: absolute;
        top:50%;
        margin-top: -0.22rem;
        right: 0rem;
        letter-spacing:-0.015rem;
        // padding-right: 0.1rem;
        span{
          font-size: 0.24rem;
          line-height: 0.37rem;
        }
      }
    }
   
    .goods_name{
      font-size:0.28rem;
      color: #333;
      &.disabled{
        color: #ccc;
      }
      line-height: 0.36rem;
      margin-bottom: 0.2rem;
      max-height: 0.72rem;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
    .good_imgs{
      display: flex;
      align-items: center;
      margin-bottom: 0.3rem;
      img{
        width: 2.3rem;
        height: 2.3rem;
        border-radius: 0.04rem;
      }
      img+img{
        margin-left: 0.1rem;
      }
      .goods_img_box{
        width: 2.3rem;
        height: 2.3rem;
        border-radius: 0.04rem;
        overflow: hidden;
      }
      .goods_img_box+.goods_img_box{
        margin-left: 0.1rem;
      }
    }
    .other_msg{
      display: flex;
      align-items: center;
      justify-content: space-between;
      .location_msg{
        display: flex;
        align-items: center;
        img{
          width: 0.32rem;
          height: 0.32rem;
        }
        span{
          font-size:0.24rem;
          color: #999;
          line-height: 0.32rem;
          margin-left: 0.04rem;
        }
      }
    }
    .cancel_btn{
        padding:0 0.28rem;
        background-color: #fff;
        height: 0.56rem;
        border-radius: 0.28rem;
        font-size:0.26rem;
        color: #333;
        border:0.02rem solid #ccc;
    }
    
  }
}

.no_data_block{
  padding-top: 1.6rem;
}
</style>
